package cn.wolfcode.web.controller;

import cn.wolfcode.domain.City;
import cn.wolfcode.domain.Province;
import cn.wolfcode.qo.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@Controller
public class JSONController {

    @RequestMapping("/checkUsername")
    @ResponseBody
    public JsonResult checkUsername(String username){
        JsonResult jsonResult = new JsonResult();
        if("lily".equals(username)){
            jsonResult.setSuccess(false);
            jsonResult.setMsg("你已经注册过了");
        }else {
            jsonResult.setSuccess(true);
            jsonResult.setMsg("恭喜你，注册成功");
        }
        return jsonResult;
    }

  /*  *//**
     * 响应json数据
     * 若存在{“username”：true，“msg”：“恭喜登录成功”}
     *
     * @param username
     * @return
     */
    @RequestMapping("/loginJson")
    @ResponseBody
    public JsonResult loginJson(String username,String password){
        JsonResult jsonResult = new JsonResult();
        if("lily".equals(username) && "123".equals(password)){
            jsonResult.setSuccess(true);
            jsonResult.setMsg("恭喜登录成功");
        }else {
            jsonResult.setSuccess(false);
            jsonResult.setMsg("登录失败");
        }
        return jsonResult;
    }

    @RequestMapping("/provinces")
    @ResponseBody
    public List<Province> getProvince(){
        List<Province> list = Province.getAllProvide();
        return list;
    }


    @RequestMapping("/city")
    @ResponseBody
    public List<City> getCityByProvinceId(Long pid){
        return City.getCityByProvinced(pid);

    }
    //你在处理方式中形参是HTTPServletResponse 这个类型，方法返回类型吧
   //响应{"success":true,"msg":"当前时间"}
    @RequestMapping("/getTime")
    public void getTime(HttpServletResponse response) throws Exception{
        response.setContentType("application/json;charset=utf-8");
       PrintWriter writer =  response.getWriter();
       writer.write("{\"success\":true,\"msg\":\""+new Date().toLocaleString()+"\"}");
       //输入流的关闭
       writer.flush();

    }


}
